/*  Copyright © 2009 Blake Hegerle -*- C++ -*- */

/*  This file is part of metal.
 
    Metal is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see
    <http://www.gnu.org/licenses/>.

*/

#include <cstddef>
#include <cstdlib>
#include <memory>

namespace metal {
  template<typename T>
  class sequence {
  public:
    typedef T value_type;
    typedef const T* pointer;
    typedef pointer const_iterator;
    typedef const_iterator iterator;
    typedef T& reference;
    typedef const T& const_reference;
    typedef ptrdiff_t difference_type;
    typedef size_t size_type;

    sequence(std::size_t size)
      : sz(size),
	rc(1),
	buffer(std::malloc(size * sizeof(T)))
    {}

    sequence(const sequence &that) 
      : sz(that.sz),
	rc(that.rc + 1),
	buffer(that.buffer)
    {}
      
    sequence(const sequence &&that)
      : sz(that.sz),
	rc(that.rc),
	buffer(that.buffer)
    {}

    ~sequence() {
      if(!--rc)
	std::free(buffer);
    }

    const_iterator begin() const { return buffer; }
    const_iterator end() const { return buffer + sz; }

    size_type size() const { return sz; }
    bool empty() const { return sz == 0; }

    // random-access container
    const_reference operator[](size_type i) { return buffer[i]; }
  private:
    std::size_t sz, rc;
    T *buffer;
  };
}
